//#include "gl_incAll.h"
#include "ddl.h"
#include "gpio.h"
#include "uart.h"
#include "uart_conf.h"
#include "string.h"
#include "stdlib.h"

#define UART_TX_PORT    GpioPortB    //UART
#define UART_TX_PIN     GpioPin8
#define UART_TX_AF      GpioAf7
#define UART_RX_PORT    GpioPortB    //UART
#define UART_RX_PIN     GpioPin9
#define UART_RX_AF      GpioAf7
/*****UART测试数据长度*****/
#define TRUE_NAMESIZE   50

char* true_filename;
char* uart0_TxData;
int txdata_len = 1;
int uart0_TxCnt = 0;    //第一个数据已经发送，从而触发发送中断

void uart_gpioconf(void)
{
    stc_gpio_cfg_t stcGpioCfg;

    DDL_ZERO_STRUCT(stcGpioCfg);

    Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE); //

    ///<TX
    stcGpioCfg.enDir = GpioDirOut;
    stcGpioCfg.enOD  = GpioOdEnable;                      //GPIO开漏输出使能
    stcGpioCfg.enPu  = GpioPuEnable;                      //必须设置成上拉，否则不能正常工作
    Gpio_Init(UART_TX_PORT, UART_TX_PIN, &stcGpioCfg);
    Gpio_SetAfMode(UART_TX_PORT, UART_TX_PIN, UART_TX_AF); //

    ///<RX
    stcGpioCfg.enDir = GpioDirIn;
    Gpio_Init(UART_RX_PORT, UART_RX_PIN, &stcGpioCfg);
    Gpio_SetAfMode(UART_RX_PORT, UART_RX_PIN, UART_RX_AF);          
}

void uart_conf(void)
{
    stc_uart_cfg_t stcCfg;
    DDL_ZERO_STRUCT(stcCfg);

    ///< 开启外设时钟
    Sysctrl_SetPeripheralGate(SysctrlPeripheralUart0, TRUE); ///<

    ///<UART Init
    stcCfg.enRunMode        = UartMskMode1;          //模式1
    stcCfg.enStopBit        = UartMsk1bit;           //1bit停止位
    //stcCfg.enMmdorCk        = UartMskEven;           //偶检验
    stcCfg.stcBaud.u32Baud  = 115200;                  //波特率115200
    stcCfg.stcBaud.enClkDiv = UartMsk8Or16Div;       //通道采样分频配置
    stcCfg.stcBaud.u32Pclk  = Sysctrl_GetPClkFreq(); //获得外设时钟（PCLK）频率值
    Uart_Init(M0P_UART0, &stcCfg);                   ///<UART0串口初始化

    //Uart_HdModeEnable(M0P_UART0);                    //使能UART0单线半双工模式
    Uart_ClrStatus(M0P_UART0, UartRC); //清除接收&发送请求
    Uart_ClrStatus(M0P_UART0, UartTC); //
    //Uart_EnableIrq(M0P_UART0,UartRxIrq); //接收中断
    //Uart_EnableIrq(M0P_UART0, UartTxIrq); //发送中断
    EnableNvic(UART0_2_IRQn, IrqLevel3, TRUE);       ///<系统中断使能

    //为uart0中断发送开辟内存空间
    true_filename = (char*)malloc(sizeof(char) * TRUE_NAMESIZE);
    uart0_TxData = (char*)malloc(sizeof(char) * (TRUE_NAMESIZE+10));
    memset(true_filename,0,TRUE_NAMESIZE);
    memset(uart0_TxData,0,TRUE_NAMESIZE+10);    //清空数据内存
}

void UART_send_bytes(unsigned char *data, unsigned char len)
{
    for (int i = 0; i < len; i++)
    {
        if ((Uart_GetStatus(M0P_UART0, UartFE)) || (Uart_GetStatus(M0P_UART0, UartPE))) //
        {
            Uart_ClrStatus(M0P_UART0, UartFE); //
            Uart_ClrStatus(M0P_UART0, UartPE); //
        }
        Uart_SendDataPoll(M0P_UART0, data[i]); //
    }
}


//UART0中断服务函数
void Uart0_IRQHandler(void)
{
    if(Uart_GetStatus(M0P_UART0, UartTC))    //发送完成标志为1
    {
        Uart_ClrStatus(M0P_UART0, UartTC);   //清空
        if(uart0_TxCnt < txdata_len)
        {
            Uart_SendDataIt(M0P_UART0, uart0_TxData[uart0_TxCnt]);//UART0发送数据
            uart0_TxCnt++;
        }
        else
        {
            uart0_TxCnt = 0;                 //发送完成，允许发送下一个调用的数据
            Uart_DisableIrq(M0P_UART0, UartTxIrq);                //禁止UART0发送功能
        }            
    }

}